import sys
import os
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '..')))
from agent import WindowAgent
import time

class TaskAutomation:
    def __init__(self):
        self.agent = WindowAgent()
    
    def open_notepad_and_write(self, text):
        """打开记事本并写入文本"""
        print("执行任务: 打开记事本并写入文本")
        
        # 直接启动记事本
        if self.agent.start_app("notepad.exe"):
            # 等待记事本窗口出现
            if not self.agent.wait_for_window("记事本", timeout=5):
                print("错误: 记事本未打开")
                return False
            
            time.sleep(1)
            
            # 验证当前窗口
            active_window = self.agent.get_active_window_title()
            if not active_window or "记事本" not in active_window:
                print(f"错误: 当前窗口不是记事本 ({active_window})")
                return False
            
            print(f"已验证窗口: {active_window}")
            
            # 输入文本
            self.agent.type_text(text)
            time.sleep(0.5)
            
            # 保存文件 (Ctrl+S)
            self.agent.send_keys(['ctrl', 's'])
            time.sleep(1)
            
            # 输入文件名
            self.agent.type_text("auto_created.txt")
            time.sleep(0.5)
            
            # 按回车保存
            self.agent.send_keys('enter')
            
            print("任务完成: 文件已保存")
            return True
        return False
    
    def calculator_task(self, expression):
        """打开计算器并计算表达式"""
        print(f"执行任务: 计算 {expression}")
        
        # 启动计算器
        if self.agent.run_program("calc.exe"):
            # 等待计算器窗口
            if not self.agent.wait_for_window("计算", timeout=5):
                print("错误: 计算器未打开")
                return False
            
            time.sleep(1)
            
            # 验证当前窗口
            active_window = self.agent.get_active_window_title()
            if not active_window or "计算" not in active_window:
                print(f"错误: 当前窗口不是计算器 ({active_window})")
                return False
            
            print(f"已验证窗口: {active_window}")
            
            # 输入计算表达式
            for char in expression:
                if char == '+':
                    self.agent.send_keys('plus')
                elif char == '-':
                    self.agent.send_keys('minus')
                elif char == '*':
                    self.agent.send_keys('multiply')
                elif char == '/':
                    self.agent.send_keys('divide')
                elif char == '=':
                    self.agent.send_keys('enter')
                else:
                    self.agent.send_keys(char)
                time.sleep(0.2)
            
            print("计算完成")
            return True
        return False
    
    def screenshot_task(self, filename="task_screenshot.png"):
        """截屏任务"""
        print("执行任务: 截屏")
        
        screenshot = self.agent.screenshot(filename)
        if screenshot:
            print(f"截屏保存为: {filename}")
            return True
        return False
    
    def batch_window_management(self):
        """批量窗口管理"""
        print("执行任务: 窗口管理")
        
        # 获取当前窗口列表
        windows = self.agent.get_window_list()
        print(f"当前窗口: {windows}")
        
        # 最小化所有窗口 (Win+M)
        self.agent.send_keys(['win', 'm'])
        time.sleep(1)
        
        # 显示桌面后再恢复 (Win+D)
        self.agent.send_keys(['win', 'd'])
        
        print("窗口管理完成")
        return True

def main():
    automation = TaskAutomation()
    
    # 执行各种自动化任务
    automation.open_notepad_and_write("这是自动创建的文件\n当前时间: " + str(time.ctime()))
    time.sleep(2)
    
    automation.calculator_task("123+456=")
    time.sleep(2)
    
    automation.screenshot_task()
    time.sleep(1)
    
    automation.batch_window_management()

if __name__ == "__main__":
    main()